/*
 * PJedit.java
 *
 * Created on 14 de agosto de 2008, 11:10
 */

package l2bot.interfaz.dialogs;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;
import java.io.IOException;
//import org.jdom.*;

import l2bot.Idioma;
import l2bot.network.login.LoginCrypt;
import org.w3c.dom.*;
import org.xml.sax.SAXException;




/**
 *
 * @author  carl
 */


public class PJedit extends javax.swing.JDialog {
    Element nodo;
    
    /** Creates new form PJedit */
    public PJedit(java.awt.Frame parent, boolean modal, Element eleme, boolean nuevo) {
        super(parent, modal);
        initComponents();
        nodo = eleme;
        textField1.setText("");
        textField1.setEchoChar('*');
        cargarServidores();
        if(nuevo){
            jTextField1.setText("");
            setTitle("Nueva cuenta");
            jButton1.setText("Nuevo");
        }else{
            jTextField1.setText(eleme.getAttribute("user"));
            textField1.setText(xord(eleme.getAttribute("pass")));
            choice1.select(eleme.getAttribute("server"));
            setTitle("Editar cuenta");
            jButton1.setText("Guardar");
        }

        
    }
    
    
    void cargarServidores(){
        DOMParser parser = new DOMParser();
        try {
            parser.parse("xml/servers.xml");
        } catch (SAXException ex) {
            
        } catch (IOException ex) {
            
        }
        Document doc = parser.getDocument();
        NodeList f = doc.getElementsByTagName("server");
        for(int i=0;i<f.getLength();i++){
            Element h = (Element) f.item(i);
            choice1.addItem(h.getAttribute("name"));
        }
    }
    
    String xore(String s){
        byte[] g = s.getBytes();
        String en = "";
        for(int i =0; i<g.length;i++){
            g[i] ^= 0x0f;
        }
        return LoginCrypt.byteArrayToHexString(g);
    }
     String xord(String s){
         s = s.replace(" ","");
        byte[] bts = new byte[s.length() / 2];
        for (int i = 0; i < bts.length; i++) {
           bts[i] = (byte) Integer.parseInt(s.substring(2*i, 2*i+2), 16);
           bts[i] ^= 0x0f;
        }
        return new String(bts);
    }   


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textField1 = new java.awt.TextField();
        checkbox1 = new java.awt.Checkbox();
        jTextField1 = new javax.swing.JTextField();
        choice1 = new java.awt.Choice();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        textField1.setText("textField1");

        checkbox1.setLabel(Idioma.mostrar);
        checkbox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                checkbox1PropertyChange(evt);
            }
        });

        jTextField1.setText("jTextField1");

        jButton1.setText(Idioma.aceptar);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addComponent(choice1, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkbox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(checkbox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void checkbox1PropertyChange(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_checkbox1PropertyChange
textField1.setEchoChar(checkbox1.getState()?(char)0:'*');
}//GEN-LAST:event_checkbox1PropertyChange

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    nodo.setAttribute("user", jTextField1.getText());
    nodo.setAttribute("pass", xore(textField1.getText()));
    nodo.setAttribute("server", choice1.getSelectedItem());
    setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Checkbox checkbox1;
    private java.awt.Choice choice1;
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    private java.awt.TextField textField1;
    // End of variables declaration//GEN-END:variables

}
